package com.awplanet.sitegen.utils;

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * User: a_polyakov
 */
public class IOUtils {

	public static void write(String data, File file) {
		try {
			if (data != null) {
				if (!file.getParentFile().isDirectory())
					file.getParentFile().mkdirs();
				FileOutputStream outputStream = new FileOutputStream(file);
				outputStream.write(data.getBytes());
				outputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String readFile(String fileName) {
		StringBuilder sb = new StringBuilder();
		try {
			FileReader fileReader = new FileReader(fileName);

			char charBuffer[] = new char[512];
			int l;
			while ((l = fileReader.read(charBuffer)) > 0) {
				sb.append(charBuffer, 0, l);
			}
			fileReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	private static final int FILE_COPY_BUFFER_SIZE = 512;

	public static void copy(File srcFile, File destFile) {
		if (srcFile.isFile()) {
			FileInputStream fis = null;
			FileOutputStream fos = null;
			FileChannel input = null;
			FileChannel output = null;
			try {

				fis = new FileInputStream(srcFile);
				fos = new FileOutputStream(destFile);
				input = fis.getChannel();
				output = fos.getChannel();
				long size = input.size();
				long pos = 0;
				long count;
				while (pos < size) {
					count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
					pos += output.transferFrom(input, pos, count);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					output.close();
				} catch (IOException e) {
				}
				try {
					fos.close();
				} catch (IOException e) {
				}
				try {
					input.close();
				} catch (IOException e) {
				}
				try {
					fis.close();
				} catch (IOException e) {
				}
			}

			if (srcFile.length() != destFile.length()) {
				System.out.println("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
			}
			destFile.setLastModified(srcFile.lastModified());
		} else if (srcFile.isDirectory()) {
			if (!destFile.exists()) {
				destFile.mkdir();
			}
			for (File srcSubFile : srcFile.listFiles()) {
				copy(srcSubFile, new File(destFile, srcSubFile.getName()));
			}
		}
	}

	public static String fileName(String fileName) {
		char array[] = fileName.trim().toLowerCase().toCharArray();
		for (int i = 0; i < array.length; i++) {
			final char c = array[i];
			if (c <= 32 || c == '\"' || c == '*' || c == '/' || c == ':' || c == '<' || c == '>' || c == '?' || c == '\\' || c == '`' || c == '|') {
				array[i] = '_';
			}
		}
		return new String(array);
	}

	public static FileOutputStream getFileOutputStream(File file) throws FileNotFoundException {
		if (!file.getParentFile().isDirectory())
			file.getParentFile().mkdirs();
		return new FileOutputStream(file);
	}
}
